package net.lunjin.controller;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.lunjin.bean.BlackList;
import net.lunjin.bean.Information;
import net.lunjin.bean.LookWant;
import net.lunjin.bean.News;
import net.lunjin.bean.NewsRemarkTemplate;
import net.lunjin.bean.Organizations;
import net.lunjin.bean.Remark;
import net.lunjin.bean.RemarkTemplate;
import net.lunjin.service.BlackListService;
import net.lunjin.service.InformationService;
import net.lunjin.service.LookWantService;
import net.lunjin.service.NewsRemarkService;
import net.lunjin.service.NewsRemarkTemplateService;
import net.lunjin.service.NewsService;
import net.lunjin.service.OrganizationsService;
import net.lunjin.service.PeopleService;
import net.lunjin.service.RemarkService;
import net.lunjin.service.RemarkTemplateService;
import net.lunjin.util.ProjectUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import finaler.util.DateUtil;
import finaler.util.Page;
import finaler.util.PageList;

@Controller
@RequestMapping("finaler")
public class FinalerController {

	@Autowired
	private OrganizationsService organizationsService;
	
	@Autowired
	private LookWantService lookWantService;

	@Autowired
	private InformationService informationService;

	@Autowired
	private NewsRemarkTemplateService newsRemarkTemplateService;

	@Autowired
	private RemarkTemplateService remarkTemplateService;
	
	@Autowired
	private NewsService newsService;

	@Autowired
	private ServletContext servletContext;
	
	@Autowired
	private NewsRemarkService newsRemarkService;

	@Autowired
	private RemarkService remarkService;

	@Autowired
	private PeopleService peopleService;

	@Autowired
	private BlackListService blackListService;

	@RequestMapping()
	public String index() {
		return "/finaler/login";
	}
	
	@RequestMapping( "/login" )
	public String login(HttpServletRequest request) {
		if( request.getParameter("finalername").equalsIgnoreCase("finaler")&&
				request.getParameter("finalerpsw").equalsIgnoreCase("1982o7o9") ){
			request.getSession().setAttribute("finaler", "finaler");
			return "/finaler/menu";
		}else{
			return "/finaler/login";
		}
	}
	
	@RequestMapping( "/auditOrg" )
	public String auditOrg(HttpServletRequest request) throws Exception {
		List<Organizations> organizationsList = organizationsService.getAuditList();
		request.setAttribute("organizationsList",organizationsList);
		return "/finaler/auditOrg";
	}

	@RequestMapping( "/modiOrg_{orgId}" )
	public String modiOrg(@PathVariable BigDecimal orgId,HttpServletRequest request) throws Exception {
		Organizations organizations  = organizationsService.findByKey(orgId);
		request.setAttribute("organizations",organizations);
		int existNum = organizationsService.getExistNum(orgId,organizations.getOrgshortname());
		request.setAttribute("existNum",existNum);
		return "/finaler/modiOrg";
	}
	
	@RequestMapping( "/saveOrg_{orgId}" )
	public String saveOrg(@PathVariable BigDecimal orgId,HttpServletRequest request) throws Exception {
		Organizations org  = new Organizations();
		org.setId(orgId);
		org.setOrgname(request.getParameter("orgname"));
		org.setOrgshortname(request.getParameter("orgshortname"));
		org.setOrgcontent(request.getParameter("orgcontent").replaceAll("\n", "<br/>"));
		org.setOrgtel(request.getParameter("orgtel"));
		org.setOrgaddr(request.getParameter("orgaddr"));
		org.setHit(new BigDecimal(request.getParameter("hit")));
		org.setWhencreate(request.getParameter("whencreate"));
		org.setWeb(request.getParameter("web"));

		org.setComin(new BigDecimal(request.getParameter("comin")));
		org.setIscom(new BigDecimal(request.getParameter("iscom")));
		org.setAble(new BigDecimal(request.getParameter("able")));
		org.setCombg(new BigDecimal(request.getParameter("combg")));
		org.setSecu(new BigDecimal(request.getParameter("secu")));
		org.setSpeed(new BigDecimal(request.getParameter("speed")));
		org.setForuser(new BigDecimal(request.getParameter("foruser")));
		
		org.setOrgvip(new BigDecimal(request.getParameter("orgvip")));
		org.setAuditFlag(new BigDecimal(request.getParameter("auditflag")));
		organizationsService.update(org);
		request.setAttribute("result", "交易平台修改成功!!!");
		return "/finaler/menu";
	}

	@RequestMapping( "/delOrg_{orgId}" )
	public String delOrg(@PathVariable BigDecimal orgId,HttpServletRequest request) throws Exception {
		Organizations org  = new Organizations();
		org.setId(orgId);
		organizationsService.delete(org);
		remarkService.deleteByOrgid(orgId);
		peopleService.deleteByOrgid(orgId);
		request.setAttribute("result", "交易平台删除成功!!!");
		return "/finaler/menu";
	}

	@RequestMapping( "/infoManage" )
	public String infoManage(HttpServletRequest request) throws Exception {
		Page page = new Page(request);
		page.setRange(20);
		PageList informationList = informationService.getAllInfoList(page);
		request.setAttribute("informationList", informationList);
		return "/finaler/infoManage";
	}
	
	@RequestMapping( "/addInfo" )
	public String addInfo(HttpServletRequest request) throws Exception {
		Information information = new Information();
		information.setCreatetime(ProjectUtil.getCurrentFormatStr("yyyy-MM-dd HH:mm:ss"));
		information.setIfcontent(request.getParameter("ifcontent").replaceAll("\n", "<br/>"));
		information.setOrgid(new BigDecimal(request.getParameter("orgid")));
		information.setAuditFlag(new BigDecimal(1));
		informationService.insert(information);
		return "redirect:/finaler/infoManage";
	}

	@RequestMapping( "/delInfo_{infoId}" )
	public String delInfo(@PathVariable BigDecimal infoId) throws Exception {
		Information information  = new Information();
		information.setId(infoId);
		informationService.delete(information);
		return "redirect:/finaler/infoManage";
	}

	@RequestMapping( "/newsBbsManage" )
	public String newsBbsManage(HttpServletRequest request) throws Exception {
		Page page = new Page(request);
		page.setRange(20);
		PageList newsList = newsService.findAll(page);
		request.setAttribute("newsList", newsList);
		return "/finaler/newsBbsManage";
	}

	@RequestMapping( "/addNews" )
	public String addNews(HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(request.getParameter("newstype").equals("22")){//标签要校验重复
			if( newsService.findByTitle(request.getParameter("newstitle").trim(),22)!=null ){
				response.setContentType("text/html; charset=utf-8");
				response.getWriter().print("<script>alert('标签已存在!!');window.history.go(-1);</script>");
				return null;
			}
		}
		News news = new News();
		news.setNewstitle(request.getParameter("newstitle"));
		news.setNewstype(new BigDecimal(request.getParameter("newstype")));
		news.setNewscontent(request.getParameter("newscontent").replaceAll("\n", "<br/>"));
		news.setCreatetime(ProjectUtil.getCurrentFormatStr("yyyy-MM-dd HH:mm:ss"));
		newsService.insert(news);
		return "redirect:/finaler/newsBbsManage";
	}
	
	@RequestMapping( "/modiNews" )
	public String modiNews(HttpServletRequest request,HttpServletResponse response) throws Exception {
		News news = new News();
		news.setId(new BigDecimal(request.getParameter("newsid")));
		news.setHit(new BigDecimal(request.getParameter("hit")));
		newsService.update(news);
		return "redirect:/finaler/newsBbsManage";
	}
	
	@RequestMapping( "/delNewsBbs_{id}" )
	public String delNewsBbs(@PathVariable BigDecimal id) throws Exception {
		newsRemarkService.deleteRemark(id);
		News news = new News();
		news.setId(id);
		newsService.delete(news);
		return "redirect:/finaler/newsBbsManage";
	}

	@RequestMapping( "/RMTempManage" )
	public String RMTempManage(HttpServletRequest request) throws Exception {
		List<RemarkTemplate> remarkTemplateList = remarkTemplateService.getAll();
		request.setAttribute("remarkTemplateList", remarkTemplateList);
		return "/finaler/RMTempManage";
	}
	
	@RequestMapping( "/delRMTemp_{rmtpId}" )
	public String delRMTemp(@PathVariable BigDecimal rmtpId) throws Exception {
		RemarkTemplate rmtp = new RemarkTemplate();
		rmtp.setId(rmtpId);
		remarkTemplateService.delete(rmtp);
		return "redirect:/finaler/RMTempManage";
	}

	@RequestMapping( "/addRMTemp" )
	public String addRMTemp(HttpServletRequest request) throws Exception {
		RemarkTemplate rmtp = new RemarkTemplate();
		rmtp.setYcoin(new BigDecimal(request.getParameter("ycoin")));
		rmtp.setDcoin(new BigDecimal(request.getParameter("dcoin")));
		rmtp.setRcoin(new BigDecimal(request.getParameter("rcoin")));
		rmtp.setKcoin(new BigDecimal(request.getParameter("kcoin")));
		rmtp.setRmcontent(request.getParameter("rmcontent"));
		remarkTemplateService.insert(rmtp);
		return "redirect:/finaler/RMTempManage";
	}
	

	@RequestMapping( "/BBSRMTempManage" )
	public String BBSRMTempManage(HttpServletRequest request) throws Exception {
		List<NewsRemarkTemplate> newsRemarkTemplateList = newsRemarkTemplateService.getAll();
		request.setAttribute("newsRemarkTemplateList", newsRemarkTemplateList);
		return "/finaler/BBSRMTempManage";
	}
	
	@RequestMapping( "/delBBSRMTemp_{rmtpId}" )
	public String delBBSRMTemp(@PathVariable BigDecimal rmtpId) throws Exception {
		NewsRemarkTemplate rmtp = new NewsRemarkTemplate();
		rmtp.setId(rmtpId);
		newsRemarkTemplateService.delete(rmtp);
		return "redirect:/finaler/BBSRMTempManage";
	}

	@RequestMapping( "/addBBSRMTemp" )
	public String addBBSRMTemp(HttpServletRequest request) throws Exception {
		NewsRemarkTemplate rmtp = new NewsRemarkTemplate();
		rmtp.setRmcontent(request.getParameter("rmcontent"));
		newsRemarkTemplateService.insert(rmtp);
		return "redirect:/finaler/BBSRMTempManage";
	}

	@RequestMapping( "/lookwantManage" )
	public String lookwantManage(HttpServletRequest request) throws Exception {
		List<LookWant> lookWantList = lookWantService.getAll();
		request.setAttribute("lookWantList", lookWantList);
		return "/finaler/lookwantManage";
	}
	
	@RequestMapping( "/delLookWant_{rmtpId}" )
	public String delLookWant(@PathVariable BigDecimal rmtpId) throws Exception {
		LookWant lookWant = new LookWant();
		lookWant.setId(rmtpId);
		lookWantService.delete(lookWant);
		return "redirect:/finaler/lookwantManage";
	}

	@RequestMapping( "/addLookWant" )
	public String addLookWant(HttpServletRequest request) throws Exception {
		LookWant lookWant = new LookWant();
		lookWant.setOrgid(new BigDecimal(request.getParameter("orgid")));
		lookWant.setPassword(request.getParameter("password"));
		lookWant.setCreatetime(ProjectUtil.getCurrentFormatStr("yyyy-MM-dd"));
		lookWantService.insert(lookWant);
		return "redirect:/finaler/lookwantManage";
	}

	@RequestMapping( "/createNews" )
	public String createNews(HttpServletRequest request) throws Exception {
		String file_real_path = servletContext.getRealPath("WEB-INF"+File.separator+"jsp"+File.separator+"news");
		newsService.autoCreateNewsPage(file_real_path);
		request.setAttribute("result", "生成新闻成功.");
		return "/finaler/menu";
	}
	
	@RequestMapping( "/createCalendar" )
	public String createCalendar(HttpServletRequest request) throws Exception {
		String file_real_path = servletContext.getRealPath("WEB-INF"+File.separator+"jsp"+File.separator+"calendar");
		newsService.autoCreateCalendarPage(file_real_path);
		request.setAttribute("result", "生成财经日历成功.");
		return "/finaler/menu";
	}
	
	@RequestMapping( "/job1hour" )
	@ResponseBody
	public void job1hour(HttpServletRequest request) throws Exception {
		//peopleService.randomWant();//每小时增加一个想入户数
		peopleService.deleteLowComWant();//删除低分的想入户人
		String file_real_path = servletContext.getRealPath("WEB-INF"+File.separator+"jsp"+File.separator+"news");
		newsService.autoCreateNewsPage(file_real_path);
		String file_real_path2 = servletContext.getRealPath("WEB-INF"+File.separator+"jsp"+File.separator+"calendar");
		newsService.autoCreateCalendarPage(file_real_path2);
	}

	@RequestMapping( "/job5minute" )
	@ResponseBody
	public void job5minute(HttpServletRequest request) throws Exception {
		//remarkService.autoRemarkCoin();//每5分钟自动发一条评分
		newsRemarkService.autoBbsRemark();//每5分钟自动发一条论坛评论
		organizationsService.autoUpdateHit();//自动更新浏览数
		newsService.autoUpdateHit();//自动更新浏览数
	}

	@RequestMapping( "/postRemark" )
	public String postRemark(HttpServletRequest request) throws Exception {
		return "/finaler/postRemark";
	}
	
	@RequestMapping( value="/remark",method=RequestMethod.POST )
	public String remark(HttpServletRequest request) throws Exception {
		Remark remark = new Remark();
		remark.setRmcontent(request.getParameter("rmcontent").replaceAll("<", "＜").replaceAll("\n", "<br/>"));//内容
		remark.setAutoflag(new BigDecimal(0));//正常发
		remark.setOrgid(new BigDecimal(request.getParameter("orgId")));
		remark.setYcoin(new BigDecimal(request.getParameter("ycoin")));
		remark.setDcoin(new BigDecimal(request.getParameter("dcoin")));
		remark.setRcoin(new BigDecimal(request.getParameter("rcoin")));
		remark.setKcoin(new BigDecimal(request.getParameter("kcoin")));
		remark.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		remark.setCreateuser(request.getParameter("ip"));
		remarkService.insert(remark);
		return "redirect:/company/"+request.getParameter("orgId")+".html#remarkDiv"; 
	}
	
	@RequestMapping( "/blackListManage" )
	public String blackListManage(HttpServletRequest request) throws Exception {
		List<BlackList> blackList = blackListService.getAll();
		request.setAttribute("blackList", blackList);
		return "/finaler/blackListManage";
	}
	
	@RequestMapping( "/addBlackList" )
	public String addBlackList(HttpServletRequest request) throws Exception {
		BlackList blackList = new BlackList();
		blackList.setIp(request.getParameter("ip"));
		blackListService.insert(blackList);
		return "redirect:/finaler/blackListManage";
	}
	
	@RequestMapping( "/delBlackList_{blId}" )
	public String delBlackList(@PathVariable BigDecimal blId) throws Exception {
		BlackList blackList = new BlackList();
		blackList.setId(blId);
		blackListService.delete(blackList);
		return "redirect:/finaler/blackListManage";
	}

	@RequestMapping( "/rmManage" )
	public String rmManage(HttpServletRequest request) throws Exception {
		Page page = new Page(request);
		page.setRange(20);
		PageList remarkList = remarkService.getRemarkList(page);
		request.setAttribute("remarkList", remarkList);
		return "/finaler/rmManage";
	}
	
	@RequestMapping( "/delRm_{rmId}" )
	public String delRm(@PathVariable BigDecimal rmId) throws Exception {
		Remark remark = new Remark();
		remark.setId(rmId);
		remarkService.delete(remark);
		return "redirect:/finaler/rmManage";
	}

	
}
